package com.apache.flow.core.dao;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.apache.core.orm.Page;
import com.apache.core.orm.hibernate.ApacheBaseDao;
import com.apache.flow.core.entity.FlowHistroy;
import com.apache.tools.StrUtil;

@Repository
public class FlowHistoryDao extends ApacheBaseDao<FlowHistroy, String> {

	/**
	 * description:  根据条件查询历史记录
	 * @param map
	 * @return  
	 * @author xyc 
	 * @update 2015-5-25
	 */
	public Page<FlowHistroy> getHistoryByInfo(Map map, int pageNo, int pageSize) {
		Page<FlowHistroy> page = new Page<FlowHistroy>();
		page.setPageNo(pageNo);
		page.setPageSize(pageSize);

		StringBuffer sb = new StringBuffer("from FlowHistroy where 1=1 ");

		String assignee = (String) map.get("assignee");
		String formId = (String) map.get("formId");
		String formDay = (String) map.get("formDay");
		String formName = (String) map.get("formName");
		String formType = (String) map.get("formType");
		String nextUser = (String) map.get("nextUser");
		String processinsId = (String) map.get("processinsId");
		String state = (String) map.get("state");
		String sysEname = (String) map.get("sysEname");
		String taskName = (String) map.get("taskName");
		String userName = (String) map.get("userName");
		//String createTime = (String) map.get("createTime");

		if (StrUtil.isNotNull(userName)) {
			sb.append(" and hisUserName like '%").append(userName).append("%'");
		}
		if (StrUtil.isNotNull(nextUser)) {
			sb.append(" and hisNextUser like '%").append(nextUser).append("%'");
		}
		if (StrUtil.isNotNull(formId)) {
			sb.append(" and hisFormId like '%").append(formId).append("%'");
		}
		if (StrUtil.isNotNull(formDay)) {
			sb.append(" and hisFormDay ='").append(formDay).append("'");
		}
		if (StrUtil.isNotNull(formType)) {
			sb.append(" and hisFormType ='").append(formType).append("'");
		}
		if (StrUtil.isNotNull(formName)) {
			sb.append(" and hisFormName like '%").append(formName).append("%'");
		}
		if (StrUtil.isNotNull(sysEname)) {
			sb.append(" and hisSysEname like '%").append(sysEname).append("%'");
		}
		//		if (StrUtil.isNotNull(createTime)) {
		//			sb.append(" and hisCreateTime like '%").append(createTime).append("%'");
		//		}
		if (StrUtil.isNotNull(assignee)) {
			sb.append(" and hisAssignee like '%").append(assignee).append("%'");
		}
		if (StrUtil.isNotNull(state)) {
			sb.append(" and hisState like '%").append(state).append("%'");
		}
		if (StrUtil.isNotNull(taskName)) {
			sb.append(" and hisTaskName like '%").append(taskName).append("%'");
		}
		if (StrUtil.isNotNull(processinsId)) {
			sb.append(" and hisProcessinsId like '%").append(processinsId).append("%'");
		}
		sb.append("order by hisCreateTime desc ");
		return this.findPage(page, sb.toString(), new HashMap<String, String>());
	};

	public int delFlowHisByProcessinsId(String hisProcessinsId) {

		String slq = "delete from FLOW_HISTROY t where t.HIS_PROCESSINS_ID ='" + hisProcessinsId + "'";
		return this.getSession().createSQLQuery(slq).executeUpdate();
	}
}
